package com.luxx.controller;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class CounterServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = getServletContext();
        Integer count = null;
        synchronized (context) {
            count = (Integer) context.getAttribute("counter");
            if (null == count) {
                count = new Integer(1);
            } else {
                count = new Integer(count.intValue() + 1);
            }
            context.setAttribute("counter", count);
        }
        resp.setContentType("text/html;charset=gb2312");
        PrintWriter out = resp.getWriter();
        out.println("");
        out.println("该页面已被访问了" + "" + count + "" + "次");
        out.close();
    }
}  